package com.briup.cms.exception;

import com.briup.cms.utils.Message;
import com.briup.cms.utils.MessageUtil;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局异常处理
 */
@RestControllerAdvice
public class GlobalExceptionHandler {

    // 捕获 Controller 中抛出的指定类型的异常，也可以指定其他异常
    @ExceptionHandler(value =  Exception.class)
    public Message handler(Exception exception){
        exception.printStackTrace();
        if(exception instanceof CustomerException){
            CustomerException customerException = (CustomerException) exception;
            if(customerException.getMessageStatus()!=null){
                return MessageUtil.error(customerException.getMessageStatus());
            }
        }
        return MessageUtil.error("后台接口异常！"+ exception.getMessage());
    }
}
